import * as fs from 'fs';
import * as path from 'path';
import * as electron from 'electron'

// ES6 Module
import * as menu from './menu';
import { editorHistory, TestCommand, AbstractCommand } from './history';
import { AbstractPropertyItemRenderer, createPropertyItemRenderer } from './propertyitemrenderer';
import { project } from './project';
import { changeDataInspector, getInspector, DataInspector } from './dataInspector';
import { initPreviewBoard, openPreviewPanel, openMapEditor, openPreviewDebugPanel } from './mainboard';
import { currentData } from './currentdata';

menu.run();


initPreviewBoard();

export interface PropertyMetadata {

    key: string,
    description: string,
    type: "string" | 'number' | "dropdown" | "primarykey" | "image" | "judge",
    default: any;
    options?: {
        filename: string,
        prefix: string
    }
}


export class ModifyPropertyCommand extends AbstractCommand {


    constructor(private obj: any, private to: any, private from: any, private key: string, private inspector: DataInspector, private renderer: AbstractPropertyItemRenderer) {
        super();
    }


    execute(): void {
        this.obj[this.key] = this.to;
        this.inspector.updateItem(this.obj);
    }
    revert(): void {
        this.obj[this.key] = this.from;
        this.renderer.updateView(this.from);
    }
}

export interface DataMetadata {
    filename: string;
    prefix: string;
    title: string;
    propertyMetadatas: PropertyMetadata[]
}

const missionMetaData: DataMetadata = {

    filename: "mission.json",
    title: "任务编辑器",
    prefix: "tasks",
    propertyMetadatas: [
        {
            key: 'id',
            description: "ID",
            default: "0",
            type: "primarykey"
        },
        {
            key: 'name',
            description: "任务标题",
            default: "新的任务",
            type: "string"
        },
        {
            key: 'content',
            description: "任务内容",
            default: "任务内容",
            type: "string"

        },
        {
            key: 'conditon',
            description: "接受任务需要完成的任务",
            default: 1,
            type: "dropdown",
            options: {
                filename: 'assets/config/mission.json',
                prefix: "tasks"
            }
        },
        {
            key: 'fromNpcId',
            description: "接受任务的NPCID",
            default: 1,
            type: "dropdown",
            options: {
                filename: 'assets/config/npc.json',
                prefix: "npc"

            }
        },

        {
            key: 'dialogbox',
            description: "对话框",
            default: "",
            type: "image"



        }
    ]
}


const npcMetadata: DataMetadata = {
    filename: "npc.json",
    prefix: 'npc',
    title: "NPC编辑器",
    propertyMetadatas: [
        {
            key: 'id',
            description: "ID",
            default: "0",
            type: "primarykey"
        },
        {
            key: 'name',
            description: "NPC姓名",
            type: "string",
            default: "无名氏"
        },
        {
            key: 'image.imageId',
            description: "NPC头像id",
            type: "primarykey",
            default: 0
        },
        {
            key: 'image.imageUrl',
            description: "NPC头像",
            type: "image",
            default: ""
        },

        {
            key: "avatar",
            description: "NPC模型",
            type: "image",
            default: ""
        }
    ]
}

const tileMetaData: DataMetadata = {
    filename: "tile.json",
    prefix: 'tile',
    title: "砖块编辑器",
    propertyMetadatas: [
        {
            key: 'id',
            description: "ID",
            default: "0",
            type: "primarykey"
        },
        {
            key: 'picture',
            description: "砖块图片",
            type: "image",
            default: ""
        },
        {
            key: 'canWalk',
            description: "是否能走",
            type: "judge",
            default: "ture"
        },
    ]
}
const mapMetadata: DataMetadata = {
    filename: "map.json",
    prefix: 'map',
    title: "地图编辑器",
    propertyMetadatas: [
    ]
}
const toolMetaData: DataMetadata = {
    filename: "tool.json",
    prefix: 'tool',
    title: "道具编辑器",
    propertyMetadatas: [
        {
            key: 'id',
            description: "ID",
            default: "0",
            type: "primarykey"
        },
        {
            key: 'name',
            description: "道具名",
            type: "string",
            default: "无敌戒指"
        },
        {
            key: 'viewS',
            description: "道具小图",
            type: "image",
            default: ""
        },
        {
            key: 'viewM',
            description: "道具中图",
            type: "image",
            default: ""
        },
        {
            key: 'viewL',
            description: "道具大图",
            type: "image",
            default: ""
        },
        {
            key: 'instruction',
            description: "道具详情",
            type: "string",
            default: "让人无敌"
        },
    ]
}

const metadatas: DataMetadata[] = [
    missionMetaData,
    npcMetadata,
    toolMetaData,
    tileMetaData,
    mapMetadata

]


const buttonGroup = document.getElementById("buttonGroup");
if (buttonGroup) {
    for (let metadata of metadatas) {
        const button = document.createElement("button");
        button.innerHTML = metadata.title;
        buttonGroup.appendChild(button);
        button.onclick = () => {
            currentData.setCurrentMetaData(metadata);
            if (metadata.prefix == "map") {
                openMapEditor();
                changeDataInspector(null);
            }
            else {
                openPreviewPanel(true);
                changeDataInspector(metadata)
            }

        }
    }
}

export function save() {
    currentData.saveAndReload();
}



const button = document.getElementById("openDebugPanelButton") as HTMLButtonElement;
button.onclick = () => {

    openPreviewDebugPanel();


}


openPreviewPanel(false);